$(function () {

    var flag = false;
    $('.apply1').on('click', function () {
        if (!(/^1[34578]\d{9}$/.test($('.f-phone').val()))) {
            alert('请填写正确手机号码');
            return false;
        }
        if ($('.f-code').val() === '') {
            alert('请填写验证码');
            return false;
        }
        if (!flag) {
            alert('请通过滑动验证码');
            return false;
        }
        if ((!(/^(0|\+?[1-9][0-9]*)$/).test($('.f-zmf').val())) || !($('.f-zmf').val() >= 400 && $('.f-zmf').val() <= 900)) {
            alert('请输入400-900范围内的芝麻分');
            return false;
        }
        if ($('.f-name').val() == '') {
            alert('请输入您的姓名');
            return false;
        }

        else {
            $.ajax({
                type: "post",
                dataType: 'json',
                contentType: 'application/json',
                url: '/cus/signUp',
                async: true,
                data: JSON.stringify({
                    "customer": {
                        "cusPhone": $('.f-phone').val(),
                        "cusSource": $('.f-zmf').val(),
                        "cusName": $('.f-name').val(),
                    },
                    "code": $('.f-code').val()
                }),
                success: function (result) {
                    if (result.code === 200) {
                        alert("操作成功");
                        $.cookie("customerId", result.data.pkGuid);
                        location.href = "/register/secondstep";
                    } else {
                        alert(result.msg);

                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    // var resultJson = eval('(' + XMLHttpRequest.responseText + ')');
                    console.log(XMLHttpRequest.responseText);
                }
            });
        }
    });


    jigsaw.init(document.getElementById('captcha'), function () {
        //验证成功后的事件自定义
        flag = true;
        $('.tcaptcha p').text('验证成功!');
        $('.tcaptcha').css('border-color', '#37dd00');
        $('.tcaptcha p').css('color', '#37dd00');
        $('.tcaptcha .bb1').addClass('gou');
        $('.tcaptcha .bb1').removeClass('cha');
        $('.bb2').remove();
        setTimeout(function () {
            $('.hdyzm').hide();
            $('#mask').hide();
        }, 500)
    }, function () {
        //验证失败后的事件自定义
        flag = false;
        $('.tcaptcha p').text('验证失败！');
        $('.tcaptcha').css('border-color', 'red');
        $('.tcaptcha p').css('color', 'red');
        $('.tcaptcha .bb1').addClass('cha');
        $('.tcaptcha .bb1').removeClass('gou');
        $('.bb2').remove();
    });


    //获取验证码
    $('.hq-code').on('click', function () {
        if (!$('.f-phone').val()) {
            alert('请填写手机号码')
        } else {
            if (!(/^1[34578]\d{9}$/.test($('.f-phone').val()))) {
                alert('请填写正确手机号码')
            } else {
                $.get('/cus/sendMeg?phone=' + $('.f-phone').val(), function (res) {
                    console.log(res)
                    if (res.code === 200) {
                        alert('验证码已发送！注意获取');
                    } else if (res.code === 201) {
                        alert('该手机已注册');
                    }
                })

            }
        }
    })


    $('.tcaptcha').on('click', function () {
        if (!flag) {
            $('.tcaptcha p').text('正在验证');
            $('.hdyzm').fadeIn(200);
            $('#mask').fadeIn(200);
        }

    });

    $('#mask').on('click', function () {
        $('.hdyzm').hide();
        $('#mask').hide();

    });

});

function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2])
    return null;
}


var countdown = 60;

function settime(val) {
    if (!$('.f-phone').val()) {
    } else {
        if (!(/^1[34578]\d{9}$/.test($('.f-phone').val()))) {
        } else {
            if (countdown == 0) {
                val.removeAttribute("disabled");
                val.value = "获取验证码";
                countdown = 60;
            } else {
                val.setAttribute("disabled", true);
                val.value = "重新发送(" + countdown + ")";
                countdown--;
                setTimeout(function () {
                    settime(val)
                }, 1000)
            }
        }
    }

}
